package ap1112.ga1.rocco;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class AP1112HS1ab {

	public static void main(String[] args) {

		// DUMMY-DATE for kw39 and year2011
		GregorianCalendar gc = new GregorianCalendar(2011, 8, 29);
		// --------------------------------------------------------

		long a = generateCode(9999, gc.getTime());

		System.out.println("Code: " + a);
		String g = getLand(a);
		System.out.println("Land: " + g);

	}

	public static long generateCode(int kdnr, Date date) {

		char[] c = getHerkunft(kdnr);
		int c1 = (int) c[0];
		int c2 = (int) c[1];
		int c3 = (int) c[2];
		String wwyyyy = getKWJahr(date);
		int ww = Integer.valueOf(wwyyyy.substring(0, 2));
		int yyyy = Integer.valueOf(wwyyyy.substring(2));
		// right calculation, but wrong output
		// long l = (((c1 * 91 + c2) * 91 + c3) * 54 + ww) * 2300 + yyyy;
		long l = ((c1 * 91 + c2) * 91 + c3) * 54 + ww;
		l = l * 2300 + yyyy;
		return l;

	}

	public static char[] getHerkunft(int kdnr) {
		// DUMMY
		char[] c = { 'G', 'E', 'R' };

		return c;
	}

	public static String getKWJahr(Date date) {

		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		int kw = gc.get(GregorianCalendar.WEEK_OF_YEAR);
		StringBuilder str = new StringBuilder();
		str.append(kw);
		str.append(gc.get(Calendar.YEAR));
		return str.toString();

	}

	private static String getLand(long a) {

		a = (a - (a % 2300)) / 2300;
		a = (a - (a % 54)) / 54;
		long c3 = a % 91;
		a = (a - c3) / 91;
		long c2 = a % 91;
		long c1 = (a - c2) / 91;
		StringBuilder str = new StringBuilder();
		str.append((char) c1);
		str.append((char) c2);
		str.append((char) c3);

		return str.toString();
	}
}
